<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page session="true" %>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LIST</title>
<link type="text/css" rel="stylesheet" href="../css/gobal.css" />
<link type="text/css" rel="stylesheet" href="../css/list.css" />
<style type="text/css">
input{
	height:18px;
}
</style>
</head>
<body>
<div id="container">
	<div id="header">
    	<div class="soh1"><img src="css/images/header.gif"></div>
        <div class="soh2">
        	<div class="ssoh21"><p>硬件列表</p></div>
        	<div class="ssoh22">
        		<p>当前用户：${user==null?"游客":user}→<a href="exit.jsp">退出</a>→<a href="index.jsp">返回主页</a></p>
        	</div>
    	</div>
    </div>
<%
	/*
	* 分页(所有的跳转传2个参数当前页码[pageNum]和总页数[pageCount])
	*/
	int pageSize=5;
	int pageNum=1;
	int pageCount=1;
	if(request.getParameter("pageNum")!=null){
		pageNum=Integer.valueOf(request.getParameter("pageNum"));
	}
	if(request.getParameter("pageCount")!=null){
		pageCount=Integer.valueOf(request.getParameter("pageCount"));
	}
	if(pageNum <= 0){
		pageNum = 1;
	}
	if(pageNum > pageCount){
		pageNum = pageCount;
	}
	/*
	 * 连接数据库[Oracle]
	 */
	String oracledriver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@//localhost:1521/orcloa";
	String username = "DONG";
	String password = "jqbXD2012";
	Connection conn = null;
	PreparedStatement pst = null;
	ResultSet rs = null;

	String sql = "SELECT BOOKID,BOOKNAME,AUTHOR,PUBLICHOUSE,PRICE,QUANTITY FROM"+
				 "(SELECT BOOKID,BOOKNAME,AUTHOR,PUBLICHOUSE,PRICE,QUANTITY,"+
				 "ROWNUM RN FROM BOOKINFO WHERE ROWNUM<="+pageSize*pageNum+"ORDER BY BOOKID) T WHERE T.RN>"+pageSize*(pageNum-1);
	try {
		Class.forName(oracledriver);
		conn = DriverManager.getConnection(url, username, password);
		rs=conn.prepareStatement("SELECT COUNT(*) FROM BOOKINFO").executeQuery();
		if(rs.next()){
			pageCount=rs.getInt(1);
			if(pageCount%pageSize!=0){
				pageCount=pageCount/pageSize+1;
			}else{
				pageCount=pageCount/pageSize;
			}
		}
		pst = conn.prepareStatement(sql);
		rs = pst.executeQuery();
	%>
    <div id="main">
    	<div class="table">
        	<div class="trh"><div class="tdh1">${msg }</div><div class="tdh2"><p><a href="listcart.jsp">查看购物车</a>←(<%=session.getAttribute("size")==null?"0":session.getAttribute("size") %>/50)→<a href="clearcart.jsp">清空购物车</a></p></div></div>
        	<div class="tr">
            	<div class="tdh">编号</div>
                <div class="tdh">书名</div>
                <div class="tdh">作者</div>
                <div class="tdh">出版社</div>
                <div class="tdh">价格</div>
                <div class="tdh">库存</div>
                <div class="tdh">数量</div>
                <div class="tdh">TAKE</div>
                <div class="tdh">BUY</div>
            </div>
		<%while(rs.next()) {%>
			<div class="tr">
            	<div class="td"><%=rs.getString("BOOKID") %></div>
                <div class="td"><%=rs.getString("BOOKNAME") %></div>
                <div class="td"><%=rs.getString("AUTHOR") %></div>
                <div class="td"><%=rs.getString("PUBLICHOUSE") %></div>
                <div class="td">$<%=rs.getFloat("PRICE") %></div>
                <div class="td"><%=rs.getInt("QUANTITY") %></div>
                <div class="tdform">
	                <form action="addtocart.jsp" method="post">
					<div class="td">
						<input type="hidden" name="bookid" value="<%=rs.getString("BOOKID") %>" />
						<input type="hidden" name="pageNum" value="<%=pageNum %>" />
						<input type="hidden" name="pageCount" value="<%=pageCount %>" />
						<select name="num" style="width:90px;"><option value="1">1</option>
   					  	<option value="2">2</option><option value="3">3</option></select></div>
                	<div class="td"><input type="image" src="css/images/cart.gif" style="padding-top:1px;" title="put into yr cart"/></div>       
                    </form>
                </div>
                <div class="td"><a href="buytocart.jsp?bookid=<%=rs.getString("BOOKID") %>"><img src="css/images/buy.gif" style="padding-top:1px;" title="buy"/></a></div>
       		</div>
		<%}%>
		<div class="tr">
            	<div class="tdf1">
                    <a href="list.jsp?pageNum=1&pageCount=<%=pageCount %>">首页</a>|
                    <a href="list.jsp?pageNum=<%=pageNum-1 %>&pageCount=<%=pageCount %>">上一页</a>|
                    <a href="list.jsp?pageNum=<%=pageNum+1 %>&pageCount=<%=pageCount %>">下一页</a>|
                    <a href="list.jsp?pageNum=<%=pageCount %>&pageCount=<%=pageCount %>">末页</a>
                    |当前页：第<%=pageNum %>/<%=pageCount %>页
                </div>
                <div class="tdf2">
                <form action="list.jsp" method="post">
                    <div class="sotf21">
						<input type="hidden" name="pageCount" value="<%=pageCount %>" />
                                                               去第<select style="width:40px" name="pageNum">
					    <%for(int i=1;i<=pageCount;i++){%>
					    	<option value=<%=i%>><%=i%></option>
					    <%}%></select>页
                    </div>
                    <div class="sotf22"><input type="image" src="css/images/go.gif" style="padding-top:1px;" title="Go"></div>
				</form>
                </div>
            </div>
        </div>
    </div>
	<%
	} catch (Exception e) {
		out.print("connect failed...");
	} finally {
		rs.close();
		pst.close();
		conn.close();
	}
%>
    <div id="footer">
    	<div class="f1"><p>Copyright©2011-06-17 Jqb</p></div>
    </div>
</div>
</body>
</html>